<template>
  <div class="list-page">
    <tool-bar nav="" title="用户">
      <el-button @click="add_click()">添加</el-button>
    </tool-bar>
    <my-list
        ref="list"
        url="/admin/admin-user/list"
        @param="preload"
        @loaded="procData"
        :page-size="10"
        :store="true"
        delete-url="/admin/admin-user/delete/{id}"
    >
      <el-table-column label="姓名" align="center" width="120">
        <template v-slot="scope">
          <div class="link" @click="edit_click(scope.row)">{{ scope.row.name }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="登录名" prop="loginName" width="120"></el-table-column>
      <el-table-column align="center" label="岗位" prop="duty" width="120"></el-table-column>

      <el-table-column align="left" label="角色" width="160">
        <template v-slot="scope">
          <el-tag v-for="(item) in scope.row.roles" :key="item.id" style="margin-right:8px;">{{ item.name }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="left" label="产品线">
        <template v-slot="scope">
          <!--                    <el-tag v-for="(item) in scope.row.productLines" @click="productLine_click(item.id)" :key="item.id" style="margin-right:8px;">{{ item.name }}</el-tag>-->
          <el-tag class="link" v-for="(item) in scope.row.productLines" :key="item.id"
                  @click="$router.push('/dev/product-line/edit/'+ item.id)" style="margin-right:8px;">{{
              item.name
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template v-slot="scope">
          <el-button plain @click="pwd_click(scope.row)" size="small">
            重置密码
          </el-button>
        </template>
      </el-table-column>
      <template #query="scope">
        <kv label="岗位">
          <selector v-model="scope.query.duty" url="/admin/admin-user/dutys"/>
        </kv>
        <kv label="名称">
          <el-input v-model="scope.query.name"></el-input>
        </kv>
      </template>
    </my-list>
  </div>
</template>

<script setup>
/**
 * Created by CodeGenerator at 2021-03-15 10:31:23
 */


//定义 vue2的this,当前组件
const $self = getCurrentInstance();

//定义 $attrs
const $attrs = useAttrs()

//定义属性
const $props = defineProps({
  id: {type: String},
  action: {type: String}
});

const $model = defineModel({type: String});

//定义data
const $data = reactive({
  isAdmin: jv.me.user.isAdmin,
});

const list = ref();

const productLine_click = function (id) {
  jv.$router.push("/dev/product-line/edit/" + id)
}
//处理请求参数
const preload = function (param) {
  //添加查询参数，修改分页参数等。
  //param.type = jv.$route.params.type
}
//处理列表的数据
const procData = function (res) {
  res.data.data.forEach(it => {
    //如果是组合实体，设置 it.id=...
    it.roles_res = (it.roles || []).map(it => it.name).join(",")
  });
}
const add_click = function () {
  jv.$router.push("/admin/admin-user/add")
}
const edit_click = function (row) {
  //记录上次点击行
  var rowId = row.id;
  list.value.setLastRow(row);
  jv.$router.push("/admin/admin-user/edit/" + rowId)
}
const pwd_click = function (row) {
  var userId = row.id;
  var userName = row.name;

  jv.prompt("请输入 " + userName + " 的密码").then(res => {
    if (!res.value) {
      return jv.alert("请输入密码!");
    }

    jv.$http.post("/admin/admin-user/reset-pwd/" + userId, {password: res.value}).then(res => {
      jv.info(userName + " 的密码已重置为 " + res.data.data)
    });
  })
}
</script>
